package com.zrz.controller;

import com.zrz.XmlUtils.CheckUtil;
import com.zrz.XmlUtils.MessageUtil;
import com.zrz.XmlUtils.XmlUtil;
import com.zrz.model.auto.User;
import com.zrz.service.UserService;
import com.zrz.service.WechatService;
import com.zrz.util.LogUtils;
import org.apache.ibatis.annotations.Param;
import org.dom4j.DocumentException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Map;

@Controller
@RequestMapping("/wechart")
public class WechartController {
    @Value("${appId}")
    private String appId;
   @Autowired
   private UserService userService;
   @Autowired
   private WechatService wechatService;
    /**
     * 统一下单接口
     * @param
     * @return
     */
    @RequestMapping("/preRecharge")
    @ResponseBody
    public Object preRecharge(HttpServletRequest request,Integer type){
        Integer userId = (Integer) request.getSession().getAttribute("userId");
        User user=userService.getUserById(userId);
        String hostAddress = "";
        try {
            InetAddress address = InetAddress.getLocalHost();
            hostAddress = address.getHostAddress();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return userService.preRecharge( user, hostAddress,type);

    }

    /**
     * 异步通知
     * @param request
     * @return
     */
    @RequestMapping(value = "/rechargeNotify")
    @ResponseBody
    public String rechargeNotify(HttpServletRequest request) {
        LogUtils.info("支付异步回调通知开始====");
        return userService.rechargeNotify(request);
    }
    /**
     *
     *wx.config
     * @param
     * @return
     */
    @RequestMapping(value = "/config")
    @ResponseBody
    public Map config(@Param("url") String url) {
        return userService.getConfig(url);
    }

    /**
     * 创建菜单
     */
    @RequestMapping(value = "/createMenu")
    @ResponseBody
    public int createMenu(){
        return wechatService.createMenu();
    }

    @RequestMapping(value = "/payConfig")
    @ResponseBody
    public Map payConfig(@Param("url") String url) {
        return userService.getConfig(url);
    }


}
